package com.evernote.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.evernote.entity.Content;
import com.evernote.entity.NoteBook;
import com.evernote.service.ContentService;
import com.evernote.service.NoteBookService;

/**
 * Servlet implementation class ToIndex
 */
@WebServlet("/ToIndex")
public class ToIndex extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ToIndex() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		
		NoteBook n = new NoteBook();
		NoteBookService nbs = new NoteBookService();
		List<NoteBook> nlist = new ArrayList<>();
		Content c = new Content();
		ContentService cs = new ContentService();
		List<Content> clist = new ArrayList<>();
//		List<Content> clistByNid = new ArrayList<>();
		
		int Uid = 0;
		try {
			Uid = (int) request.getAttribute("Uid");
		} catch (NullPointerException e) {
			Uid = Integer.parseInt(request.getParameter("Uid"));
		}
		
		n.setUid(Uid);
		nlist = nbs.findAllNotebookByUid(n);
		c.setUid(Uid);
		
		int noteNum = 0;
		int defaultNoteNid = nbs.findDefaultNoteIdByUid(n);
		int selectedNid = 0;
		try {
			selectedNid = (int) request.getAttribute("selectedNid");
		} catch(NullPointerException e) {
			selectedNid = 0;
		}
		if(selectedNid == 0) {
			selectedNid = defaultNoteNid;
		} else {
			defaultNoteNid = selectedNid;
		}
		n.setId(selectedNid);
		String name = nbs.findNameById(n);
		String notebookMsg = "所有笔记";
		
		clist = (List<Content>) request.getAttribute("clist");
		if(clist == null) {
			clist = cs.findAllContentByUid(c);
			noteNum = cs.findAllContentNumByUid(c);
		} else {
			try {
				int Nid = Integer.parseInt(request.getParameter("Nid"));
				defaultNoteNid = Nid;
				c.setNid(Nid);
				noteNum = cs.findAllNoteNumByNid(c);
				notebookMsg = name;
			} catch(NumberFormatException e) {
				
			}
		}
		
		boolean isNotebookShow = false;
		try {
			isNotebookShow = (boolean) request.getAttribute("isNotebookShow");
		} catch(NullPointerException e) {
			isNotebookShow = false;
		}
		
		request.setAttribute("isNotebookShow", isNotebookShow);
		request.setAttribute("notebookMsg", notebookMsg);
		request.setAttribute("name", name);
		request.setAttribute("Uid", Uid);
		request.setAttribute("selectedNid", selectedNid);
		request.setAttribute("nlist", nlist);
		request.setAttribute("clist", clist);
		request.setAttribute("noteNum", noteNum);
		request.setAttribute("Nid", defaultNoteNid);
		request.getRequestDispatcher("WEB-INF/jsp/index.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
